package com.component.chat;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.opengl.Visibility;
import android.view.View;
import android.widget.Toast;

import com.easemob.helpdesk.activity.MainActivity;
import com.easemob.helpdesk.utils.CommonUtils;
import com.easemob.helpdesk.utils.DialogUtils;
import com.facebook.react.ReactApplication;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.google.gson.JsonObject;
import com.housekeeperapp.MainApplication;
import com.hyphenate.kefusdk.HDDataCallBack;
import com.hyphenate.kefusdk.chat.HDClient;
import com.reactnativenavigation.NavigationApplication;
import com.reactnativenavigation.bridge.BundleConverter;
import com.reactnativenavigation.controllers.NavigationActivity;
import com.reactnativenavigation.controllers.NavigationCommandsHandler;

/**
 * Created by wsj on 2017/10/17.
 */
public class EasemobChatModule extends ReactContextBaseJavaModule {

    private Context mContext;
    private ReactApplication reactApplication;
    private static final String ACTIVITY_PARAMS_BUNDLE_PAGE = "ACTIVITY_PARAMS_BUNDLE_PAGE";
    private int isShow = 0;

    public EasemobChatModule(ReactApplicationContext reactContext, ReactApplication mainApplication) {
        super(reactContext);
        this.reactApplication = mainApplication;

//        mContext = reactContext;
    }

    @Override
    public String getName() {
        return "EasemobChat";
    }

    @ReactMethod
    public void isLoggedInBefore(Callback callback) {
        boolean logged = HDClient.getInstance().isLoggedInBefore();
        callback.invoke(logged);
    }

    @ReactMethod
    public void chatLogin(ReadableMap params, final Callback callback) {
        // 在线登录
        HDClient.getInstance().login(params.getString("uName"), params.getString("uPwd"), false, new HDDataCallBack() {
            @Override
            public void onSuccess(Object o) {
                callback.invoke("success");
            }

            @Override
            public void onError(int i, String s) {
                callback.invoke("fail");
            }

            @Override
            public void onAuthenticationException() {
                callback.invoke("fail");
            }
        });
    }

    @ReactMethod
    public void chatLogout(final Callback callback) {

        HDClient.getInstance().logout(new HDDataCallBack() {
            @Override
            public void onSuccess(Object o) {
                callback.invoke("success");
            }

            @Override
            public void onError(int i, String s) {
                callback.invoke("fail");
            }

            @Override
            public void onAuthenticationException() {
                callback.invoke("fail");
            }
        });
    }

    @ReactMethod
    public void startNativeActivity(ReadableMap params){
//        mContext = getCurrentActivity();
        Activity activity = com.housekeeperapp.MainActivity.getInstance();

        if(NavigationActivity.currentActivity != null){
            activity = NavigationActivity.currentActivity;
        }

        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("user_id", params.getInt("user_id"));
        intent.putExtra(ACTIVITY_PARAMS_BUNDLE_PAGE, BundleConverter.toBundle(params.getArray("tabs")));
        intent.setClass(activity, MainActivity.class);

        activity.startActivity(intent);
        if(activity instanceof NavigationActivity){
            activity.finish();
        }

    }

    @ReactMethod
    public void logoutActivity(){

        Activity activity;
        if(NavigationActivity.currentActivity == null){
            return;
        }

        activity = NavigationActivity.currentActivity;

        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setClass(activity,  NavigationActivity.class);

//        mContext.startActivity(intent);

        activity.startActivity(intent);
        activity.finish();

    }


    @ReactMethod
    public void showOrderFailDialog(){
        Activity activity = MainApplication.getInstance().getTopActivity();

        Dialog dialog = DialogUtils.getFailOrderDialog(activity, "");
        dialog.show();
    }

    @ReactMethod
    public void showOrderDialog(ReadableMap params) {
        Intent intent = new Intent();
        intent.setAction("com.housekeeperapp.serviceorder");

        String msg = params.getString("message");

        intent.putExtra("extras", params.getString("extras"));
        intent.putExtra("message", msg);
        if(msg.contains("快来抢单")){
            intent.putExtra("type", "serviceorder");
        }else if(msg.contains("有一个管家发起转单")){
            intent.putExtra("type", "turnorder");
        }else if(msg.contains("转单超3分钟无人抢单")){
            intent.putExtra("type", "timeoutorder");
        }else if(msg.contains("有新订单啦")){
            intent.putExtra("type", "remindorder");
        }

        if(getReactApplicationContext().hasActiveCatalystInstance()){
            MainApplication.getInstance().sendBroadcast(intent);
//            MainApplication.getInstance().getTopActivity().sendBroadcast(intent);
        }
    }

}
